package com.momoauth.common.core.config;

import com.momoauth.common.core.annotation.EnableMoMo;
import com.momoauth.common.core.filter.SensitiveWordsFilter;
import com.momoauth.common.core.filter.SqlFilter;
import com.momoauth.common.core.filter.XssFilter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration(proxyBeanMethods = false)
public class FilterConfig implements ImportBeanDefinitionRegistrar, ResourceLoaderAware {

    private static List<Object> classList = new ArrayList<>();;

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {}

    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        AnnotationAttributes annoAttrs = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableMoMo.class.getName()));
        // 多个匹配对象
        Class<?>[] excludeFilters = annoAttrs.getClassArray("excludeFilters");
        for (Object o : excludeFilters) {
            if (o != null) {
                classList.add(o);
            }
        }
        // 单个匹配对象
        String excludeFilter = annoAttrs.getString("excludeFilter");
        Class c = null;
        try {
            c = Class.forName(excludeFilter);
            classList.add(c);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Bean
    public FilterRegistrationBean<SqlFilter> sqlFilterRegistration() {
        FilterRegistrationBean<SqlFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new SqlFilter());
        filterRegistrationBean.setOrder(2);
        for (Object o : classList) {
            if (o == SqlFilter.class) {
                filterRegistrationBean.setEnabled(false);
                log.warn("SqlFilter: is disabled");
            }
        }
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<XssFilter> xssFilterRegistration() {
        FilterRegistrationBean<XssFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new XssFilter());
        filterRegistrationBean.setOrder(3);
        for (Object o : classList) {
            if (o == XssFilter.class) {
                filterRegistrationBean.setEnabled(false);
                log.warn("XssFilter: is disabled");
            }
        }
        return filterRegistrationBean;
    }

    @Bean
    public FilterRegistrationBean<SensitiveWordsFilter> sensitiveWordsFilterRegistration() {
        FilterRegistrationBean<SensitiveWordsFilter> filterRegistrationBean = new FilterRegistrationBean<>();
        filterRegistrationBean.setFilter(new SensitiveWordsFilter());
        filterRegistrationBean.setOrder(4);
        for (Object o : classList) {
            if (o == SensitiveWordsFilter.class) {
                filterRegistrationBean.setEnabled(false);
                log.warn("SensitiveWordsFilter: is disabled");
            }
        }
        return filterRegistrationBean;
    }
}
